package com.zlt.util;

import org.apache.poi.xwpf.converter.core.BasicURIResolver;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;

/**
 * @Author IBing
 * @Date 2022/8/11 23:06
 * @Version 1.0
 */
public class previewResumeUtil {
    /**
     * 生成html
     * @param username
     * @throws Throwable
     */

    public static void docxToHtml(String username) throws IOException {
        //简历的路径
        File file2 = new File("web/recruiter/layuimini/html/resume/resumeFile/");
        String path2= file2.getAbsolutePath();

        String filepath = path2;
        String fileName ="/" + username + ".docx";
        String htmlName = username + ".html";
        String filePath = filepath+fileName;
        //图片存放路径
        String imagePath = filepath + File.separator + username;
        //转换的html文件路径 与图片在同目录中
        String fileOutName = imagePath + File.separator + htmlName;
        //获取一个用操作Word的对象
        XWPFDocument document = new XWPFDocument(new FileInputStream(filePath));
        //导出为html时的一些基本设置类
        XHTMLOptions options = null;
        //判断word文件中是否有图片
        if(document.getAllPictures().size() > 0) {
            //获取默认的对象，设置缩进indent
            options = XHTMLOptions.getDefault().indent(4);
            // 如果包含图片的话，要设置图片的导出位置
            File imageFolder = new File(imagePath);
            //设置图片抽取器的目的地文件夹 用于存放图片文件
            options.setExtractor(new FileImageExtractor(imageFolder));
            // URI resolver  word的html中图片的目录路径
            options.URIResolver(new BasicURIResolver(imagePath));
        }
        //获取输出的html文件对象
        File outFile = new File(fileOutName);
        //创建所有的父路径，如果不存在父目录的话
        outFile.getParentFile().mkdirs();
        //创建一个输出流
        OutputStream out = new FileOutputStream(outFile);
        //html转换器
        XHTMLConverter.getInstance().convert(document, out, options);
    }

}

